package ExerciseClass.Exercise;

public class Test {
    public static String getTrans(int n ,int m ){
        StringBuilder str1 = null;
        String str = "";
        while (n>0){
            int temp = n % m ;
            str += temp;
            n/=m;
        }
        str1 = new StringBuilder(str);
        str1=str1.reverse();
        String str2 = new String(str1);
        return str2;
    }
    public static int getJieCeng(int n ){
        if(n==1){
            return 1;
        }else {
            return n*getJieCeng(n-1);
        }
    }
    public static int getSum1(int n ){
        if(n==1){
            return 1;
        }else return n+getSum1(n-1);
    }
    public static double getSum2(int n){
        if (n==1){
            return 1;
        }else return 1.0/n+getSum2(n-1);
    }
    public static int getFobo(int n){
        if(n==1||n==2){
            return 1;
        }else return  getFobo(n-1)+getFobo(n-2);
    }
    public static boolean getHuiwen(String s){
        for(int i = 0,j=s.length()-1;j>=0&&i<s.length()&&i!=j;j--,i++){
            if(s.charAt(j)!=s.charAt(i)){
                return false;
            }
        }
        return true;
    }
    //abc  ----> cba
    public static String getReserve(String s,int i){
        if(i==0){
            return "";
        }else{
            i--;
            return s.charAt(i)+getReserve(s,i);
        }
    }
//    public static int getHigh(){
//
//    }
    public static void main(String[] args) {
        String trans = Test.getTrans(305, 16);
        System.out.println(trans);

        int jieCeng = Test.getJieCeng(4);
        System.out.println(jieCeng);

        int sum1 = Test.getSum1(100);
        System.out.println(sum1);

        double sum2 = Test.getSum2(3);
        System.out.println(sum2);

        for (int i = 1; i <= 20; i++) {
            System.out.println("第"+i+"个元素为："+Test.getFobo(i));
        }

        boolean noon = Test.getHuiwen("anboobn");
        if(noon){
            System.out.println("是回文串");
        }else {
            System.out.println("不是回文串");
        }
        String str = "aaaafc";
        System.out.println(Test.getReserve(str, str.length()));
        Tree tree = new Tree("A");
        tree.setLeft(new Tree("B"));
        tree.getLeft().setLeft(new Tree("D"));
        tree.setRight(new Tree("C"));
        tree.getRight().setLeft(new Tree("E"));
        tree.getRight().setRight(new Tree("F"));
        tree.getLeft().getLeft().setLeft(new Tree("H"));
        tree.getRight().getRight().setLeft(new Tree("G"));
        tree.getRight().getRight().setRight(new Tree("I"));
        TreeMethod treeMethod = new TreeMethod();
        treeMethod.setRoot(tree);
        treeMethod.printOfPre();
        System.out.println();
        treeMethod.getTreemin();
        System.out.println();
        treeMethod.getAfter();
        System.out.println();
        System.out.println(treeMethod.getHigh());
        System.out.println();
        System.out.println(treeMethod.getJieDian());
        System.out.println();
        System.out.println(treeMethod.getYeZiJieDian());
    }
}
class TreeMethod{
    private Tree root;

    public void setRoot(Tree root) {
        this.root = root;
    }

    //前序
    public void printOfPre(){
        if (root!=null){
            root.getTree();
        }else {
            System.out.println("树空");
        }
    }
    //中序
    public void getTreemin(){
        if (root!=null){
            root.getTreemin();
        }else {
            System.out.println("树空");
        }
    }
    //后序
    public void getAfter(){
        if (root!=null){
            root.getAfter();
        }else {
            System.out.println("树空");
        }
    }
    public int getHigh(){
        if(root==null){
            return 0;
        }else {
            return root.getHigh(root);
        }
    }
    public int getJieDian(){
        if(root==null){
            return 0;
        }else {
            return root.getJieDian(root);
        }
    }
    public int getYeZiJieDian(){
        if(root==null){
            return 0;
        }else {
            return root.getYeZiJieDian(root);
        }
    }
}
class Tree{
    private String head;
    public Tree(String head) {
        this.head=head;
    }
    public Tree left;
    public Tree right;

    public String getHead() {
        return head;
    }

    public void setHead(String head) {
        this.head = head;
    }

    public Tree getLeft() {
        return left;
    }

    public void setLeft(Tree left) {
        this.left = left;
    }

    public Tree getRight() {
        return right;
    }

    public void setRight(Tree right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "Tree{" +
                 head +"}";
    }

    //前序遍历
    public void getTree(){
        System.out.println(this);
        if(this.getLeft()!=null){
            this.getLeft().getTree();
        }
        if(this.getRight()!=null){
            this.getRight().getTree();
        }
    }
    //中序
    public void getTreemin(){
        if(this.getLeft()!=null){
            this.getLeft().getTreemin();
        }
        System.out.println(this);
        if(this.getRight()!=null){
            this.getRight().getTreemin();
        }
    }
    //后序
    public void getAfter(){
        if(this.getLeft()!=null){
            this.getLeft().getAfter();
        }
        if(this.getRight()!=null){
            this.getRight().getAfter();
        }
        System.out.println(this);
    }
    //计算高度
    public int getHigh(Tree tree){
        int l=0;
        int r=0;
        if(tree.getLeft()!=null){
            l = getHigh(tree.getLeft());
        }
        if (tree.getRight()!=null){
            r = getHigh(tree.getRight());
        }

        return l>r?l+1:r+1;
    }
    //计算结点
    public int getJieDian(Tree tree){
        int l = 0;
        int r = 0;
        if(tree.getLeft()!=null){
            l=tree.getJieDian(tree.getLeft());
        }
        if(tree.getRight()!=null){
            r=tree.getJieDian(tree.getRight());
        }
        return l+r+1;
    }
    public int getYeZiJieDian(Tree tree){
        if(tree.getLeft()==null||tree.getRight()==null){
           return 1;
        }else {
            return getYeZiJieDian(tree.getLeft())+getYeZiJieDian(tree.getRight());
        }
    }
}
